//
//  SendTableViewCell.m
//  NewChat
//
//  Created by songKX on 16/3/6.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "SendTableViewCell.h"

@interface SendTableViewCell ()

@property (nonatomic , strong) NSString *textStr;

@property(nonatomic , strong)UIImageView * chatImageMessageView;



@end



@implementation SendTableViewCell


-(UIImageView *)chatImageMessageView{
    
    if (_chatImageMessageView == nil) {
        _chatImageMessageView = [[UIImageView alloc] init];
    }
    
    return _chatImageMessageView;
    
    
}

-(void)setMessage:(EMMessage *)message{
    
    _message = message;
    
    [self.chatImageMessageView removeFromSuperview];
    
    id body = message.messageBodies[0];
    
    if ([body isKindOfClass:[EMTextMessageBody class]]) {
        
        EMTextMessageBody *textBody = body;
        
        self.messageLabel.text = textBody.text;
        
    }else if ([body isKindOfClass:[EMVoiceMessageBody class]]){
        
        self.messageLabel.attributedText  = [self vocieAttWith];
        
        
    }else if ([body isKindOfClass:[EMImageMessageBody class]]){
        
        [self showImage];
        
    }
    
}

//返回语音的富文本
-(NSAttributedString *)vocieAttWith{
    
    NSMutableAttributedString *voiceAttM = [[NSMutableAttributedString alloc] init];
    
    EMVoiceMessageBody *voiceBody = self.message.messageBodies[0];
    NSInteger duration = voiceBody.duration;
    NSString *timeStr = [NSString stringWithFormat:@"%ld \"  ",duration];
    
    NSAttributedString *timeAtt = [[NSAttributedString alloc] initWithString:timeStr];
    
    [voiceAttM appendAttributedString:timeAtt];
    
    UIImage *receiverImg = [UIImage imageNamed:@"chat_sender_audio_playing_full"];
    NSTextAttachment *imgAttachment = [[NSTextAttachment alloc] init];
    imgAttachment.image = receiverImg;
    imgAttachment.bounds = CGRectMake(0, -8, 30, 30);
    
    NSAttributedString *imagAtt = [NSAttributedString attributedStringWithAttachment:imgAttachment];
    [voiceAttM appendAttributedString:imagAtt];
    
    return [voiceAttM copy];
    
}




- (void)awakeFromNib {
    
    [self.messageLabel  addGestureRecognizer: [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)]];
    
    self.messageLabel.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width*0.65;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageLabelTap:)];
    
    self.messageLabel.userInteractionEnabled = YES;
    
    [self.messageLabel addGestureRecognizer:tap];
    
    
    
    // Initialization code
}

-(void)messageLabelTap:(UITapGestureRecognizer *)recognizer{
    
    id body = _message.messageBodies[0];
    
    if ([body isKindOfClass:[EMTextMessageBody class]]) {
        
        
        
        
    }else if ([body isKindOfClass:[EMVoiceMessageBody class]]){
        
        [SKXFriendManager playVoiceWith:self.message WithMessageLabel:self.messageLabel Bool:NO];
        
        
    }else if ([body isKindOfClass:[EMImageMessageBody class]]){
        
        
        
    }
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

+(instancetype)cellForTableView:(UITableView *)tableView{
    
    static NSString *identifer = @"seCell";
    SendTableViewCell *temp = [tableView dequeueReusableCellWithIdentifier:identifer];
    
    
    
    if (temp == nil) {
        
        temp = [[[NSBundle mainBundle] loadNibNamed:@"SendTableViewCell" owner:nil options:nil] firstObject];
        
        temp.messageLabel.userInteractionEnabled = YES;

        
    }
    return temp;
    
}

-(CGFloat)getCellHeight{
    
    [self layoutIfNeeded];
    
    
    return self.messageLabel.frame.size.height + 50;
    
    
}

-(void)showImage{
    //获取图片消息体
    EMImageMessageBody *imageBody = self.message.messageBodies[0];
    //图片缩略图的尺寸
    CGRect thumbnailFrame = (CGRect){0 , 0 , imageBody.thumbnailSize};
    
    //设置Label的尺寸足够显示UIImageView
    NSTextAttachment *imageAttach = [[NSTextAttachment alloc] init];
    imageAttach.bounds = thumbnailFrame;
    
    NSAttributedString *imageAtt = [NSAttributedString attributedStringWithAttachment:imageAttach];
    
    
    self.messageLabel.attributedText = imageAtt;
    
  
    
    self.chatImageMessageView.frame = thumbnailFrame;
    //
        [self.messageLabel addSubview:self.chatImageMessageView];
    
    //3.下载图片
    NSFileManager *manager = [NSFileManager defaultManager];
    
    UIImage *placeImage = [UIImage imageNamed:@"chatBar_more_photo"];
    
    
    //如果本地的图片存在,直接从本地显示图片
    if ([manager fileExistsAtPath:imageBody.thumbnailLocalPath]) {
        
        NSLog(@"图片存在");
        
        [self.chatImageMessageView sd_setImageWithURL:[NSURL fileURLWithPath:imageBody.thumbnailLocalPath] placeholderImage:placeImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
            
            
        }];
        
        
        
    }else{
       
        
        
        NSLog(@"图片不存在");
        //如果本地的图片不存在，
        [self.chatImageMessageView sd_setImageWithURL:[NSURL URLWithString:imageBody.thumbnailRemotePath] placeholderImage:placeImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
                        
            
        }];
    }
}


//长按现实菜单

-(void)longTap:(UILongPressGestureRecognizer *)sender{
    
    if (sender.state==UIGestureRecognizerStateBegan) {
        
        [self becomeFirstResponder];
        
        UIMenuController *menu=[UIMenuController sharedMenuController];
        
        UIMenuItem *copyItem = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copyItemClicked:)];
        
        UIMenuItem *resendItem = [[UIMenuItem alloc] initWithTitle:@"粘贴" action:@selector(resendItemClicked:)];
        
        [menu setMenuItems:[NSArray arrayWithObjects:copyItem,resendItem,nil]];
        
        [menu setTargetRect:self.messageLabel.bounds inView:self.messageLabel];
        
        [menu setMenuVisible:YES animated:YES];
        
    }
    
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    
    if(action ==@selector(copyItemClicked:)){
        
        return YES;
        
    }else if (action==@selector(resendItemClicked:)){
        
        return YES;
        
    }
    
    return [super canPerformAction:action withSender:sender];
    
}

#pragma mark 实现成为第一响应者方法

-(BOOL)canBecomeFirstResponder{
    
    return YES;
    
}

//复制方法
-(void)copyItemClicked:(id)sender{
    
    
    self.textStr = self.messageLabel.text;
    
    
}

//粘贴方法

-(NSString *)resendItemClicked:(id)sender{
    
    return self.textStr;
    
}


@end
